package com.mysterymall.Controller.Admin;

import com.mysterymall.Common.Comp.HttpStatus;
import com.mysterymall.Common.Comp.Log;
import com.mysterymall.Common.Utils.BaseEnum;
import com.mysterymall.Common.Utils.Result;
import com.mysterymall.Model.Dto.OrderDto;
import com.mysterymall.Model.Dto.UpdateOrderDto;
import com.mysterymall.Model.Dto.WithdrawDto;
import com.mysterymall.Model.Entity.WithdrawDeposit;
import com.mysterymall.Service.OrderInfoService;
import com.mysterymall.Service.PaymentService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName com.mysterymall.Controller.Admin
 * @Description
 * @Author HuangWX
 * @Time 2023/12/07  14:42
 */
@RestController
@RequestMapping("/order")
public class AdminOrderController {


    private final OrderInfoService orderInfoService;


    private final PaymentService paymentService;


    /**
     * 构造器注入
     * @param orderInfoService 订单服务
     */
    public AdminOrderController(OrderInfoService orderInfoService, PaymentService paymentService) {
        this.orderInfoService = orderInfoService;
        this.paymentService = paymentService;
    }

    /**
     * 查询所有订单
     * @param type 订单类型
     * @param currIndex 当前页
     * @param pageSize 每页条数
     * @return Result
     */
    @GetMapping("/getAllOrder")
    @Log
    public Result getAllOrder(@RequestParam("type")String type, @RequestParam("currIndex")Integer currIndex, @RequestParam("pageSize") Integer pageSize,
                              HttpServletRequest request){
        return orderInfoService.getAllOrder(type,currIndex,pageSize,request);
    }


    /**
     * 查询订单详情
     * @return Result
     */
    @GetMapping("/queryStatus")
    @Log
    public Result queryStatus(@RequestParam("orderStatus")String orderStatus,@RequestParam("currIndex")String currIndex,@RequestParam("pageSize")String pageSize){
        return orderInfoService.queryStatus(orderStatus,currIndex,pageSize);
    }


    // 修改提现申请状态
    @PostMapping("/updateWithdrawDepositStatus")
    @Log
    public Result updateWithdrawDepositStatus(@RequestBody WithdrawDto withdrawDto){

        Map<String, Object> map = orderInfoService.updateWithdrawDepositStatus(withdrawDto);

        if(map.get("Result") != null){
            return (Result) map.get("Result");
        }else{
            WithdrawDeposit withdrawDeposit = (WithdrawDeposit) map.get("withdrawDeposit");
            if (withdrawDeposit.getWithdrawStatus().equals(BaseEnum.Audit_Passed.getCode())) {
                Map<String, Object> stringObjectMap = paymentService.withdrawDeposit(withdrawDeposit);
                if(stringObjectMap.get("Result") != null){
                    return (Result) stringObjectMap.get("Result");
                }
            }else if(withdrawDeposit.getWithdrawStatus().equals(BaseEnum.Audit_Not_Passed.getCode())){
                orderInfoService.WithdrawalAndRefund(withdrawDeposit);
            }
            return new Result(HttpStatus.SUCCESS,"message.updateSuccess");
        }
    }


    // 查询提现申请列表
    @GetMapping("/getWithdrawDepositList")
    @Log
    public Result getWithdrawDepositList(@RequestParam("currIndex")Integer currIndex, @RequestParam("pageSize") Integer pageSize, @RequestParam("status")String status){
        return orderInfoService.getWithdrawDepositList(currIndex,pageSize,status);
    }

    //  修改订单状态
    @PostMapping("/updateOrderStatus")
    @Log
    public Result updateOrderStatus(@RequestBody UpdateOrderDto updateOrderDto){
        return orderInfoService.updateOrderStatus(updateOrderDto);
    }


    @GetMapping("/getPaymentOrder")
    @Log
    public Result getPaymentOrder(@RequestParam("currIndex")Integer currIndex, @RequestParam("pageSize") Integer pageSize,
                                  @RequestParam(value = "search",defaultValue = "")String search,HttpServletRequest request,
                                  @RequestParam("type")String type,@RequestParam("time")String time){
        return orderInfoService.getPaymentOrder(currIndex,pageSize,search,request,type,time);
    }
}
